package main.java;

import _61970.core.ConductingEquipment;
import _61970.core.Equipment;
import _61970.core.IdentifiedObject;
import _61970.core.Substation;
import _61970.core.Terminal;
import _61970.core.VoltageLevel;
import org.apache.log4j.Logger;
import org.eclipse.emf.ecore.resource.Resource;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 *
 * @author Wang Bin
 *         Date: 12-6-26
 */
public class DefaultResourceManager implements IResourceManager{
    public static Logger log = Logger.getLogger(DefaultResourceManager.class);

    private Resource resource;
    private Map<Class, List> classToList = new HashMap<Class, List>();
    private Map<String, IdentifiedObject> idToObject = null;
    private Map<String, Substation> name2Substation = null;

    public DefaultResourceManager(Resource resource) {
        this.resource = resource;
    }

    public List getResource(Class aClass) {
        List l = classToList.get(aClass);
        if (l != null) return l;
        //
        ArrayList myList = new ArrayList();
        List list = resource.getContents();
        for (Object obj : list) {
            if (aClass.isAssignableFrom(obj.getClass()))
                myList.add(obj);
        }
        classToList.put(aClass, myList);
        return myList;
    }

    public List getResourceAll() {
        return resource.getContents();
    }

    public IdentifiedObject getResource(String id) {
        if(idToObject == null) {
            List list = resource.getContents();
            idToObject = new HashMap<String, IdentifiedObject>(list.size());
            for (Object object : list)
                idToObject.put(((IdentifiedObject) object).getMRID(), (IdentifiedObject) object);
        }
        return idToObject.get(id);
    }

    public Substation getSubstationByName(String name) {
        if (name2Substation == null) {
            updateName2Substation();
        }
        return name2Substation.get(name);
    }

    public List getEquipmentOfStationByName(String stationName) {
        Substation substation = getSubstationByName(stationName);
        if (substation != null)
            return getEquipmentOfStation(substation);
        log.warn("can not find the station whose name is : " + stationName);
        return null;
    }

    public Map<String, IdentifiedObject> getIdToObject() {
        return idToObject;
    }

    public List<Equipment> getEquipmentOfStation(Substation station) {
        if (station == null) {
            log.warn("can not find the station!");
            return null;
        }
        List voltageLevels = station.getContains_VoltageLevels();
        if (voltageLevels == null) {
            log.warn("no voltage level found in station : " + station.getName() + " :" + station.getMRID());
            return null;
        }
        List<Equipment> l = new ArrayList<Equipment>(station.getContains_Equipments());
        for (Object o : voltageLevels) {
            List<Equipment> cnList = ((VoltageLevel) o).getContains_Equipments();
            for (Equipment equip : cnList)
                if (!l.contains(equip))
                    l.add(equip);
        }
        return l;
    }

    private void updateName2Substation() {
        name2Substation = new HashMap<String, Substation>();
        List<Substation> allSubstation = (List<Substation>) getResource(Substation.class);
        for (Substation station : allSubstation) {
            if (name2Substation.containsKey(station.getName()))
                log.warn("There is duplicated substation name :" + station.getName());
            name2Substation.put(station.getName(), station);
        }
    }


}
